package offerv1;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;

public class Offer59_2 {

}

class MaxQueue1 {
    int max;
    Deque<Integer> maxQueue;
    Queue<Integer> queue;

    public MaxQueue1() {
        max = Integer.MIN_VALUE;
        maxQueue = new LinkedList<>();
        queue = new LinkedList<>();
    }

    public int max_value() {
        return maxQueue.isEmpty() ? -1 : maxQueue.getFirst();
    }

    public void push_back(int value) {
        int maxValue = maxQueue.isEmpty() ? value : Math.max(maxQueue.getLast(), value);
        maxQueue.add(maxValue);
        queue.add(value);
    }

    public int pop_front() {
        maxQueue.poll();
        return queue.isEmpty() ? -1 : queue.poll();
    }
}

class MaxQueue {
    Queue<Integer> q;
    Deque<Integer> d;

    public MaxQueue() {
        q = new LinkedList<Integer>();
        d = new LinkedList<Integer>();
    }

    public int max_value() {
        if (d.isEmpty()) {
            return -1;
        }
        return d.peekFirst();
    }

    public void push_back(int value) {
        while (!d.isEmpty() && d.peekLast() < value) {
            d.pollLast();
        }
        d.offerLast(value);
        q.offer(value);
    }

    public int pop_front() {
        if (q.isEmpty()) {
            return -1;
        }
        int ans = q.poll();
        if (ans == d.peekFirst()) {
            d.pollFirst();
        }
        return ans;
    }
}
